package com.zxc.gulimall.product.service.impl;

import com.zxc.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zxc.gulimall.product.entity.AttrEntity;
import com.zxc.gulimall.product.service.AttrAttrgroupRelationService;
import com.zxc.gulimall.product.service.AttrService;
import com.zxc.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.zxc.gulimall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxc.common.utils.PageUtils;
import com.zxc.common.utils.Query;

import com.zxc.gulimall.product.dao.AttrGroupDao;
import com.zxc.gulimall.product.entity.AttrGroupEntity;
import com.zxc.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrService attrService;

    //根据分类Id查出所有的分组以及这些组里面的属性
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVoList = new ArrayList<>();
        //1、根据catelogId查找出所有符合条件的attr_group_id
        List<AttrGroupEntity> attrGroupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //2、在关系表中找出所有和Id相等的数据
        for (AttrGroupEntity attrGroupEntity:attrGroupEntityList) {
            //3、每一个实体类都对应的一个AttrGroupWithAttrsVo
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            //3.1把基本属性赋值到实体类
            BeanUtils.copyProperties(attrGroupEntity,attrGroupWithAttrsVo);
            //3.2根据attr_group_id查询出它对应的所有attr信息
            QueryWrapper<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityQueryWrapper = new QueryWrapper<>();
            attrAttrgroupRelationEntityQueryWrapper.eq("attr_group_id",attrGroupEntity.getAttrGroupId());
            List<AttrAttrgroupRelationEntity> attrgroupRelationEntityList = attrAttrgroupRelationService.list(attrAttrgroupRelationEntityQueryWrapper);
            if (attrgroupRelationEntityList != null && attrgroupRelationEntityList.size() > 0){
                //如果得到的关系实体类集合不为空，说明它有属性与其对应
                List<Long> attrIdList = attrgroupRelationEntityList.stream().map(attrId->{
                    return attrId.getAttrId();
                }).collect(Collectors.toList());

                List<AttrEntity> attrEntityList = attrService.listByIds(attrIdList);
                attrGroupWithAttrsVo.setAttrs(attrEntityList);
            }
            attrGroupWithAttrsVoList.add(attrGroupWithAttrsVo);
        }

        return attrGroupWithAttrsVoList;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //查出当前spu对应的所有的属性的分组信息，以及当前分组下所有属性对应的值。
        //1、当前spu有多少对应的属性分组
        List<SpuItemAttrGroupVo> spuItemAttrGroupVoList = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return spuItemAttrGroupVoList;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        Object key = params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((object) -> {
                object.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }

        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),queryWrapper);
            return new PageUtils(page);
        } else {
            //select * from pms_attr where catelog_id = ? and (id = key or name like ...)
            queryWrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
            return new PageUtils(page);
        }

        }
}




